package com.juyu.reader.pc.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.io.output.StringBuilderWriter;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreeMarkertUtil {
	/**
	 * @param templateName
	 *            模板名字
	 * @param root
	 *            模板根 用于在模板内输出结果集
	 * @param out
	 *            输出对象 具体输出到哪里
	 */
	public static void publishCategory(ServletContext context, Map map, String templatePath, String templateName,
			String htmlCategory, String categoryName, String htmlName) throws IOException, TemplateException {
		Configuration freemarkerCfg = new Configuration();
		// 设置要解析的模板所在的目录，并加载模板文件
		freemarkerCfg.setServletContextForTemplateLoading(context, templatePath);
		freemarkerCfg.setEncoding(Locale.getDefault(), "UTF-8");

		// 获取模板,并设置编码方式
		Template template = freemarkerCfg.getTemplate(templateName);
		template.setEncoding("UTF-8");
		File categoryFile = new File(context.getRealPath("") + File.separator + "html" + File.separator + "web"
				+ File.separator + htmlCategory + File.separator + categoryName);
		if (!categoryFile.exists()) {
			categoryFile.mkdirs();
		}
		// 创建文章目录
		Writer out = new BufferedWriter(
				new OutputStreamWriter(new FileOutputStream(categoryFile + File.separator + htmlName), "UTF-8"));
		// 合并数据模型与模板
		template.process(map, out);

		out.flush();
		out.close();
	}

	public static void publishHtml(ServletContext context, Map map, String templatePath, String templateName,
			String htmlCategory, String htmlName) throws IOException, TemplateException {
		Configuration freemarkerCfg = new Configuration();
		// 设置要解析的模板所在的目录，并加载模板文件
		freemarkerCfg.setServletContextForTemplateLoading(context, templatePath);
		freemarkerCfg.setEncoding(Locale.getDefault(), "UTF-8");

		// 获取模板,并设置编码方式
		Template template = freemarkerCfg.getTemplate(templateName);
		template.setEncoding("UTF-8");
		File contentFile = new File(context.getRealPath("") + File.separator + "html" + File.separator + "web"
				+ File.separator + htmlCategory + File.separator + "content");
		if (!contentFile.exists()) {
			contentFile.mkdirs();
		}
		// 创建文章内容
		Writer out = new BufferedWriter(
				new OutputStreamWriter(new FileOutputStream(contentFile + File.separator + htmlName), "UTF-8"));
		// 合并数据模型与模板
		template.process(map, out);

		out.flush();
		out.close();
	}

	public static void publishHome(ServletContext context, Map map, String templatePath, String templateName)
			throws IOException, TemplateException {
		Configuration freemarkerCfg = new Configuration();
		// 设置要解析的模板所在的目录，并加载模板文件
		freemarkerCfg.setServletContextForTemplateLoading(context, templatePath);
		freemarkerCfg.setEncoding(Locale.getDefault(), "UTF-8");

		// 获取模板,并设置编码方式
		Template template = freemarkerCfg.getTemplate(templateName);
		template.setEncoding("UTF-8");
		File contentFile = new File(context.getRealPath("") + File.separator + "ftl" + File.separator + "book");
		if (!contentFile.exists()) {
			contentFile.mkdirs();
		}
		// 创建文章内容
		Writer out = new BufferedWriter(
				new OutputStreamWriter(new FileOutputStream(contentFile + File.separator + "home.html"), "UTF-8"));
		// 合并数据模型与模板
		template.process(map, out);

		out.flush();
		out.close();
	}

	public static String getStringFromTemplate(ServletContext context, Map map, String templatePath,
			String templateName) throws IOException, TemplateException {
		Configuration freemarkerCfg = new Configuration();
		// 设置要解析的模板所在的目录，并加载模板文件
		freemarkerCfg.setServletContextForTemplateLoading(context, templatePath);
		freemarkerCfg.setEncoding(Locale.getDefault(), "UTF-8");

		// 获取模板,并设置编码方式
		Template template = freemarkerCfg.getTemplate(templateName);
		template.setEncoding("UTF-8");

		StringBuilderWriter out = new StringBuilderWriter();
		template.process(map, out);
		return out.getBuilder().toString();
	}
}
